package com.an.vhr.service;

import com.an.vhr.bean.Employee;
import com.an.vhr.bean.MailConstants;
import com.an.vhr.bean.MailSendLog;
import com.an.vhr.bean.RespPageBean;
import com.an.vhr.mapper.EmployeeMapper;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author: an1089
 **/
@Service
public class EmployeeService {
    @Resource
    EmployeeMapper employeeMapper;

    @Resource
    RabbitTemplate rabbitTemplate;

    @Resource
    MailSendLogService mailSendLogService;

    SimpleDateFormat year = new SimpleDateFormat("yyyy");
    SimpleDateFormat mouth = new SimpleDateFormat("MM");
    DecimalFormat decimalFormat = new DecimalFormat("##.00");

    public RespPageBean getEmpByPage(Integer page, Integer size, Employee employee, Date[] beginDateScope) {
        if(page != null && size != null){
            page = (page-1)*size;
        }
        List<Employee> data = employeeMapper.getEmpByPage(page,size, employee,beginDateScope);
        Long total = employeeMapper.getTotal(employee,beginDateScope);
        RespPageBean pageBean = new RespPageBean();
        pageBean.setTotal(total);
        pageBean.setData(data);
        return pageBean;
    }

    public Integer addEmployee(Employee employee) {
        Date beginContract = employee.getBeginContract();
        Date endContract = employee.getEndContract();
        double res = (Double.parseDouble(year.format(endContract)) - Double.parseDouble(year.format(beginContract))) * 12 +
                (Double.parseDouble(mouth.format(endContract)) - Double.parseDouble(mouth.format(beginContract)));
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(res/12)));
        int i = employeeMapper.insertSelective(employee);
        if(i == 1){
            Employee sendData = employeeMapper.getEmpById(employee.getId());
            String msgId = UUID.randomUUID().toString();
            MailSendLog mailSendLog = new MailSendLog();
            mailSendLog.setMsgId(msgId);
            mailSendLog.setCreateTime(new Date());
            mailSendLog.setExchange(MailConstants.MAIL_EXCHANGE_NAME);
            mailSendLog.setRouteKey(MailConstants.MAIL_ROUTING_KEY_NAME);
            mailSendLog.setEmpId(sendData.getId());
            mailSendLog.setTryTime(new Date((System.currentTimeMillis()+1000*60*MailConstants.MSG_TIMEOUT)));
            mailSendLogService.insert(mailSendLog);
            rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME,MailConstants.MAIL_ROUTING_KEY_NAME,sendData,new CorrelationData(msgId));
        }
        return i;
    }

    public Integer getMaxWorkId() {
        return employeeMapper.getMaxWorkId();
    }

    public Integer removeEmpById(Integer id) {
        return employeeMapper.deleteByPrimaryKey(id);
    }

    public Integer updateEmp(Employee employee) {
        return employeeMapper.updateByPrimaryKey(employee);
    }

    public Integer addEmps(List<Employee> list) {
        return employeeMapper.addEmps(list);
    }

    public RespPageBean getEmployeeWithSalaryPage(Integer page, Integer size) {
        if(page != null && size != null){
            page = (page-1)*size;
        }
        List<Employee> employeeList = employeeMapper.getEmployeeWithSalaryPage(page,size);
        Long total = employeeMapper.getTotal(null, null);
        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setData(employeeList);
        respPageBean.setTotal(total);
        return respPageBean;
    }

    public Employee getEmpById(Integer empId) {
        return employeeMapper.getEmpById(empId);
    }
}
